//
//  DocManager.m
//  gdemm
//
//  Created by 乔明超 on 16/4/27.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "DocManager.h"
#import "TxtHandler.h"
#import "ImgHandler.h"
#import "OfficeHandler.h"
#import "PdfHandler.h"
#import "StringUtil.h"
#import "NativeEncrypt.h"
#import "zipHandler.h"
#import "CadHandler.h"
@interface DocManager()
@property (strong,nonatomic) id<DocIntentListener> mIntentListener;
@property (strong,nonatomic) NSMutableArray *mDocHandlers;
@end
@implementation DocManager
static DocManager *mInstance;
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addFileHandlers];
    }
    return self;
}



- (NSMutableArray *)mDocHandlers{

    if (_mDocHandlers == nil) {
        _mDocHandlers = [NSMutableArray array];
    }
    return _mDocHandlers;
}
+ (instancetype)getInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mInstance = [[self alloc]init];
    });
    return mInstance;

}
- (void)addFileHandlers{
    [self addDocHandler:[[TxtHandler alloc]init]];
    [self addDocHandler:[[ImgHandler alloc]init]];
    [self addDocHandler:[[OfficeHandler alloc]init]];
    [self addDocHandler:[[PdfHandler alloc]init]];
    [self addDocHandler:[[zipHandler alloc]init]];
    [self addDocHandler:[[CadHandler alloc]init]];
}
- (void) addDocHandler:(FileHandler *)handler{
    if ([self.mDocHandlers indexOfObject:handler]== NSNotFound) {
        [self.mDocHandlers addObject:handler];
    }
    

}

- (FileHandler *)getDocHandlerWithFileName:(NSString *)fileName silent:(BOOL)silent{
    FileHandler  *fh;
    NSString *suffix = [StringUtil getSuffix:fileName];
    for (FileHandler  *handler in self.mDocHandlers) {
        if ([handler isSupportedType:suffix]) {
            fh = handler;
            return fh;
        }
        
    }
    return fh;
}

- (void)onIntentError:(NSString *)fileName :(NSString *)error{
    if (self.mIntentListener != nil) {
        [self.mIntentListener onGetIntentErrorWithFileName:fileName error:error];
    }
}

- (BOOL)isDocumentSupported:(NSString *)fileName{
    if ([self getDocHandlerWithFileName:fileName silent:YES] !=nil) {
        return YES;
    }
    return NO;
}

-(UIViewController *)getControllerForFilePath:(NSString *)filePath{
    FileHandler *handler = [self getDocHandlerWithFileName:filePath silent:NO];
    if (handler == nil) {
        return nil;
    }
    UIViewController *vc = [handler getControllerForFilePath:filePath];
    if (vc == nil) {
        [self onIntentError:filePath :[handler getErrorInfo]];
    }
    return vc;
}


- (NSString *)getIconWithFileName:(NSString *)fileName isEnced:(BOOL)isEnced{
    FileHandler *handler = [self getDocHandlerWithFileName:fileName silent:YES];
    if (handler != nil) {
        return [handler getIconWithFileName:fileName enced:isEnced];
    }
    
    return nil;
}
- (NSString *)getPathMappedIconId:(NSString *)path{
    NSString *icon;
    BOOL isEnced = ![NativeEncrypt isEncryptedFile:path];
    icon = [self getIconWithFileName:path isEnced:isEnced];
    if (icon == nil) {
        icon = isEnced ? @"cloudDisk_icon_unknown_enced" : @"cloudDisk_icon_unknown";
    }
    return icon;
}

@end
